import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;

import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class roomLayoutPanel extends JPanel implements MouseListener, MouseMotionListener{
	
	// data controls
	private roomRect[] rooms;
	private int activeId;
	private int x;
	private int y;
	private boolean dragging;
	private roomManager rm;
	private Image img;
	
	public roomLayoutPanel(roomManager initRm){
		super();
		rm = initRm;
		loadImage();
		initialize();
		this.setPreferredSize(new Dimension(850,660));
		this.setBackground(Color.WHITE);
		this.addMouseListener(this);
	}

	private void initialize() {
		// TODO Auto-generated method stub
		activeId = 0;
		dragging = false;
		x = -100;
		y = -100;
		rooms = rm.getRoomRects();
	}
	
	public void loadImage(){
		Toolkit tk = Toolkit.getDefaultToolkit();
		Image t0 = tk.createImage("transfer.jpg");
		MediaTracker tracker = new MediaTracker(roomLayoutPanel.this);
		tracker.addImage(t0, 0);
		try { 
			tracker.waitForAll(); 
		}
		catch(InterruptedException ie) {}
		img = t0;
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Rectangle2D newRect = null;
		Graphics2D g2 = (Graphics2D) g;
		for (int i=0; i<rooms.length; i++){
			newRect = new Rectangle2D.Double(rooms[i].getX(), rooms[i].getY(),
					rooms[i].getWidth(), rooms[i].getHeight());
			g2.draw(newRect);
			if (rooms[i].getRoomType() == 4)
				g2.drawString(rooms[i].getRoomName(), rooms[i].getX()+rooms[i].getWidth()/3,
					rooms[i].getY()+rooms[i].getHeight()/2);
			else if (rooms[i].getRoomType() == 3)
				g2.drawString(rooms[i].getRoomName(), rooms[i].getX()+rooms[i].getWidth()/12,
					rooms[i].getY()+rooms[i].getHeight()/2);
			else if (rooms[i].getRoomType() == 2)
				g2.drawString(rooms[i].getRoomName(), rooms[i].getX()+rooms[i].getWidth()/4,
					rooms[i].getY()+rooms[i].getHeight()/2);
			else if (rooms[i].getRoomType() == 1)
				g2.drawString(rooms[i].getRoomName(), rooms[i].getX()+rooms[i].getWidth()/3,
					rooms[i].getY()+rooms[i].getHeight()/2);
			else if (rooms[i].getRoomType() == 0)
				g2.drawString(rooms[i].getRoomName(), rooms[i].getX()+rooms[i].getWidth()/5*2,
					rooms[i].getY()+rooms[i].getHeight()/2);
		}
		//g.drawImage(img, x, y, 50, 50, null);
	}

	public void setActiveRoom(int initX, int initY){
		activeId = 0;
		x = -100;
		y = -100;
		for (int i=0; i<rooms.length-1; i++){
			roomRect temp = rooms[i];
			if (initX >= temp.getX() && initX <= temp.getX()+temp.getWidth()
					&& initY >= temp.getY() && initY <= temp.getY()+temp.getHeight()){
					activeId= temp.getRoomNum();
					x = initX-50;
					y = initY-50;
			}
		}
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (!dragging)
			setActiveRoom(arg0.getX(), arg0.getY());
		if (activeId != 0){
			if (arg0.getButton() == MouseEvent.BUTTON3){
				JOptionPane.showMessageDialog(roomLayoutPanel.this, rm.toString(rm.getRoomRects()[activeId-1].getRoomInfo()));
			}
			else if (arg0.getButton() == MouseEvent.BUTTON1){
				if (rm.getRoomRects()[activeId-1].getRoomInfo().getCurrent().getName().equals("")){
					createByClickDialog temp = new createByClickDialog(rm, activeId);
					temp.setVisible(true);
				}
				else JOptionPane.showMessageDialog(roomLayoutPanel.this, 
						"Error: This room is occupied by others now.", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		dragging = true;
		setActiveRoom(arg0.getX(), arg0.getY());
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (dragging){
			int roomNum1 = activeId;
			setActiveRoom(arg0.getX(), arg0.getY());
			int roomNum2 = activeId;
			if (rm.getRoomState()[roomNum1-1] == 1 && rm.getRoomState()[roomNum2-1] == 0)
				rm.changeRooms(roomNum1, roomNum2);
			dragging = false;
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
			updateRectLocation(e.getX(), e.getY());
	}
	
	// update the location of the selected rect
	public void updateRectLocation(int initX, int initY){
		x = initX;
		y = initY;
		repaint();
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
